package com.prabhakar;

import java.util.ArrayList;

import org.w3c.dom.Document;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;

/*rahul.chaurasia.1506@gmail.com*/

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

/*
 * Lat - 12.914788 & Long-77.610106  :BTM
 * Lat- 12.928521 & Long-77.583434 :Jayanagar
 * Lat - 12.95159 & Long- 77.59038 : KH circle
 * Lat-12.94231 & Long- 77.59739  Dairy Circle
 * */
public class MainActivity extends FragmentActivity { // Activity {

	private static final String TAG ="MainActivity";
	GoogleMap mMap;
	GMapV2Direction md;

	LatLng fromPosition = new LatLng(12.914788 , 77.610106); 			//13.687140112679154, 100.53525868803263); 
	LatLng toPosition = new LatLng(12.94231 , 77.59739); 				//13.683660045847258, 100.53900808095932); 			


	//NEEDS TO BE FIXED >> ## converting address-location into Latlng location & then passing the same..
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		md = new GMapV2Direction();
		mMap = ((SupportMapFragment)getSupportFragmentManager()
				.findFragmentById(R.id.the_map)).getMap();

		LatLng coordinates = new LatLng(12.914788 , 77.610106);		

	//	mMap.setMyLocationEnabled(true);
		mMap.getUiSettings().setZoomControlsEnabled(true);
		mMap.getUiSettings().setCompassEnabled(true);
//		mMap.getUiSettings().setMyLocationButtonEnabled(true);
		mMap.getUiSettings().setAllGesturesEnabled(true);
		mMap.setTrafficEnabled(true);
		mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
		mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 16));

		mMap.addMarker(new MarkerOptions().position(fromPosition).title("Start"));
		mMap.addMarker(new MarkerOptions().position(toPosition).title("End"));
		try{
			Document doc = md.getDocument(fromPosition, toPosition, GMapV2Direction.MODE_DRIVING);
			int duration = md.getDurationValue(doc);
			String distance = md.getDistanceText(doc);
			String start_address = md.getStartAddress(doc);
			String copy_right = md.getCopyRights(doc);
			
			Log.v(TAG ,"time duration >>"+duration);
			Log.v(TAG ,"distance >>"+distance);
			Log.v(TAG ,"start_address >>"+start_address);
			
			ArrayList<LatLng> directionPoint = md.getDirection(doc);
			PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

			for(int i = 0 ; i < directionPoint.size() ; i++) {	
				
				System.out.println("\n directionPoint's >>> "+directionPoint.get(i));
				rectLine.add(directionPoint.get(i));
			}
			mMap.addPolyline(rectLine);

		}catch(NullPointerException npe){
			npe.getMessage();
		}catch(Exception e){
			e.getMessage();
		}

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
